package com.weitu.occupationalHealth.medicalPlan.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.occupationalHealth.medicalPlan.entity.MedicalPlan;
import com.weitu.occupationalHealth.medicalPlan.service.MedicalPlanService;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/4/1.
 */
@Controller
@RequestMapping(value = "occupationalHealth/medicalPlan")
public class MedicalPalnController extends BaseController {

    @Autowired
    private MedicalPlanService medicalPlanService;

    /**
     * 跳转至体检计划页面
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public String list() {
        return BASE_JSP_PATH + "occupationalHealth/medicalPlan/list";
    }

    /**
     * 体检计划新增
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public String add() {
        return BASE_JSP_PATH + "occupationalHealth/medicalPlan/add";
    }

    /**
     * 体检批次选择
     * @return
     */
    @RequestMapping(value = "/planTb")
    public String planTb(){
        return BASE_JSP_PATH + "occupationalHealth/medicalPlan/planTb";
    }

    /**
     * 加载体检计划
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/planTable", method = RequestMethod.POST)
    public Object planTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum = this.medicalPlanService.queryPlanCount(paramMap);
        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map> dataList = this.medicalPlanService.selectAllPlans(paramMap);
        Object data = dataGridPagination.flushDataGridData(dataList);
        return data;
    }

    /**
     * 添加体检计划
     *
     * @param medicalPlan
     * @return
     */
    @ResponseBody
    @RequestMapping("insertSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "体检计划", methods = "添加体检计划")
    public AjaxResult insertSelective(MedicalPlan medicalPlan) {
        String id = IDGenerator.getId();
        medicalPlan.setId(id);
        medicalPlan.setDelState(0);
        String time = DateTimeUtil.formatterDateTime();
        medicalPlan.setAddTime(time);
        User loginUser = getSessionUser();
        String loginUserId = loginUser.getId();
        String loginUserDeptId = loginUser.getDepartment().getId();
        medicalPlan.setAddUserId(loginUserId);
        medicalPlan.setAddDeptId(loginUserDeptId);
        int boo = medicalPlanService.insertSelective(medicalPlan);
        if (boo == 1) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 删除体检计划
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "体检计划", methods = "删除体检计划")
    public AjaxResult deleteById(String id) {
        int boo = medicalPlanService.deleteById(id);
        if (boo == 1) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.fail("删除失败");
        }
    }

    /**
     * 跳转至计划编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping("edit")
    public ModelAndView edit(String id) {
        ModelAndView modelAndView = new ModelAndView();
        Map medicalPlan = medicalPlanService.selectById(id);
        modelAndView.addObject("plan", medicalPlan);
        modelAndView.setViewName(BASE_JSP_PATH + "occupationalHealth/medicalPlan/edit");
        return modelAndView;
    }

    /**
     * 跳转详情页面
     *
     * @param id
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(String id,String type) {
        ModelAndView modelAndView = new ModelAndView();
        Map medicalPlan = medicalPlanService.selectById(id);
        modelAndView.addObject("plan", medicalPlan);
        if (type.equals("info")){
            modelAndView.setViewName(BASE_JSP_PATH + "occupationalHealth/medicalPlan/info");
        }else {
            modelAndView.setViewName(BASE_JSP_PATH + "occupationalHealth/medicalPlan/edit");
        }
        return modelAndView;
    }
    /**
     * 更新计划
     *
     * @param medicalPlan
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "体检计划", methods = "更新计划")
    public AjaxResult updateByIdSelective(MedicalPlan medicalPlan) {
        String time = DateTimeUtil.formatterDateTime();
        medicalPlan.setUpdateTime(time);
        User loginUser = getSessionUser();
        String loginUserId = loginUser.getId();
        String loginUserDeptId = loginUser.getDepartment().getId();
        medicalPlan.setUpdateUserId(loginUserId);
        medicalPlan.setUpdateDeptId(loginUserDeptId);
        int boo = medicalPlanService.updateByIdSelective(medicalPlan);
        if (boo == 1) {
            return success("更新成功");
        } else {
            return fail("更新失败");
        }
    }
}
